﻿<!doctype html>
<html>
<head>
    <title>SpreadJS - Custom Functions</title>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>

    <link href="../../../external/spreadjs/css/gcspread.sheets.excel2013white.9.40.20161.0.css" rel="stylesheet" type="text/css" />

    <script src="../../../external/external/jquery-1.8.2.min.js" type="text/javascript"></script>

    <script type="text/javascript" src="../../../external/spreadjs/gcspread.sheets.all.9.40.20161.0.min.js"></script>

    <script id="scriptInit" type="text/javascript">
    /*code_begin*/
    $(document).ready(function () {
        var spread = new GcSpread.Sheets.Spread(document.getElementById("ss"), { sheetCount: 1 });
        initSpread(spread);
    });
    function initSpread(spread) {
        var spreadNS = GcSpread.Sheets;
        
        var sheet = spread.getSheet(0);

        function FactorialFunction() {
            this.name = "FACTORIAL";
            this.maxArgs = 1;
            this.minArgs = 1;
        }
        FactorialFunction.prototype = new spreadNS.Calc.Functions.Function();
        FactorialFunction.prototype.evaluate = function (args) {
            var result = 1;
            if (args.length === 1 && !isNaN(parseInt(args[0]))) {
                for (var i = 1; i <= args[0]; i++) {
                    result = i * result;
                }
                return result;
            }
            return "#VALUE!";
        }
        var factorial = new FactorialFunction();

        $("#addCustomFunction").click(function() {
            sheet.addCustomFunction(factorial);
            sheet.setFormula(1, 1, "=factorial(5)");
        });

        $("#removeCustomFunction").click(function() {
            sheet.removeCustomFunction("FACTORIAL");
        });
    };
    /*code_end*/
    </script>
</head>
<body>
    <div class="sample-turtorial">
        <div id="ss" style="width:100%; height:410px;border: 1px solid gray;"></div>
        <div class="demo-options">
            <div class="option-row">
                <input type="button" value="Add a Custom Function(factorial)" id="addCustomFunction" />
                <input type="button" value="Remove the Custom Function(factorial)" id="removeCustomFunction"/>
            </div>
        </div>
    </div>
</body>
</html>